import { _decorator, Component, EventTouch, Graphics, Node, UITransform, v2, v3, Vec2 } from 'cc';
import { LogMgr } from '../../../framework/log/LogMgr';
import { BattleRoom } from '../../funcs/battle/BattleRoom';
import { GlobalEventMgr } from '../../../framework/event/GlobalEventMgr';
import { GlobalEventTyp } from '../../GlobalEventTyp';
const { ccclass, property } = _decorator;

@ccclass('Joystick')
export class Joystick extends Component {

    //遥感按钮
    joyBtnNode:Node
    //遥感中心黑色圆
    joystickCenterNode:Node
    //触摸时的起始位置
    joyStartPos:Vec2 =  v2(0,0)
    //遥感底部半径
    joyBottomRadius:number = 143
    //遥感方向
    direction:Vec2 = v2(0,0)
    old_direction:Vec2 = v2(0,0)
    //触摸ID
    _touchId:number = null

    graph:Graphics  = null

    protected start(): void {
        this.joyBtnNode = this.node.getChildByName("joy_btn")
        this.joystickCenterNode = this.node.getChildByPath("center")
        this.graph = this.node.getComponent(Graphics)
    }


    work() {
        this.node.on(Node.EventType.TOUCH_START,(event:EventTouch)=>{
            // 如果已有触摸在进行，忽略新触摸
            if (this._touchId !== null) return;

            this._touchId = event.getID();


            let p = event.touch.getUILocation()
            let {x,y} = this.getComponent(UITransform).convertToNodeSpaceAR(v3(p.x,p.y,0))
            this.joyStartPos.set(x,y)
            this.joyBtnNode.setPosition(x,y)
            this.joystickCenterNode.setPosition(x,y)
            this.joyBtnNode.active = true
            this.joystickCenterNode.active = true
        },this)
        this.node.on(Node.EventType.TOUCH_MOVE,(event:EventTouch)=>{
            let uiPos = event.touch.getUILocation()
            let {x,y} = this.getComponent(UITransform).convertToNodeSpaceAR(v3(uiPos.x,uiPos.y,0))
            let localPos = v2(x,y)
            let len = Vec2.distance(localPos,this.joyStartPos)
            this.direction = localPos.clone().subtract(this.joyStartPos)
            // Vec2.subtract(this.direction,localPos,this.joyStartPos)
            this.direction.normalize()
            // this.graph.clear()
            // this.graph.strokeColor.fromHEX("#000")
            // this.graph.moveTo(this.joyStartPos.x,this.joyStartPos.y)
            // this.graph.lineTo(x,y)
            // this.graph.stroke()
            if(len>this.joyBottomRadius){
                let directionWithLen = v2(0,0)
                Vec2.multiplyScalar(directionWithLen,this.direction, this.joyBottomRadius)
                directionWithLen = directionWithLen.add(this.joyStartPos)
                this.joystickCenterNode.setPosition(directionWithLen.x,directionWithLen.y)
            }else{
                this.joystickCenterNode.setPosition(x,y)
            }
            if(this.old_direction.equals(this.direction)){
                return
            }
            this.old_direction.set(this.direction)
            GlobalEventMgr.ins.emit(GlobalEventTyp.DirectionChange,this.direction)
        },this)
        this.node.on(Node.EventType.TOUCH_END,(event:EventTouch)=>{
            this._touchId = null
            this.joyBtnNode.setPosition(0,0)
            this.joystickCenterNode.setPosition(0,0)
            this.joyBtnNode.active = false
            this.joystickCenterNode.active = false
            this.direction.set(0,0)
            this.old_direction.set(0,0)
            GlobalEventMgr.ins.emit(GlobalEventTyp.DirectionChange,this.direction)
        },this)
    }

    unwork() {
        this.node.off(Node.EventType.TOUCH_START)
        this.node.off(Node.EventType.TOUCH_MOVE)
        this.node.off(Node.EventType.TOUCH_END)
    }
}


